<!--
 * @Author: 高瑞寒 15932717091@163.com
 * @Date: 2022-05-17 22:48:58
 * @LastEditTime: 2022-05-20 20:56:37
 * @LastEditors: 高瑞寒 15932717091@163.com
 * @Description:
-->
<template>
  <el-dialog :model-value="modelValue" title="提示" @close="cancel" width="22%">
    <div class="center">
      <p class="title">{{ $t('theme.themeColorChange') }}</p>
      <div>
        <el-color-picker
          v-model="mColor"
          show-alpha
          :predefine="predefineColors"
        />
      </div>
    </div>
    <template #footer>
      <el-button @click="cancel">{{ $t('universal.cancel') }}</el-button>
      <el-button @click="confirm" type="primary">{{
        $t('universal.confirm')
      }}</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref } from 'vue'
import { useStore } from 'vuex'
import { generateNewStyle, writeNewStyle } from '@/utils/theme'
// eslint-disable-next-line no-undef
defineProps({
  modelValue: {
    type: Boolean,
    default: false,
    required: true
  }
})
// eslint-disable-next-line no-undef
const emits = defineEmits(['update:modelValue'])
const cancel = () => {
  emits('update:modelValue', false)
}

const store = useStore()
const mColor = ref(store.getters.mainColor)
const confirm = async () => {
  store.commit('theme/setMainColor', mColor.value)
  const newStyle = await generateNewStyle(mColor.value)
  writeNewStyle(newStyle)
  cancel()
}

const predefineColors = [
  '#ff4500',
  '#ff8c00',
  '#ffd700',
  '#90ee90',
  '#00ced1',
  '#1e90ff',
  '#c71585',
  'rgba(255, 69, 0, 0.68)',
  'rgb(255, 120, 0)',
  'hsv(51, 100, 98)',
  'hsva(120, 40, 94, 0.5)',
  'hsl(181, 100%, 37%)',
  'hsla(209, 100%, 56%, 0.73)',
  '#c7158577'
]
</script>

<style scoped lang="scss">
.center {
  text-align: center;
  .title {
    margin-bottom: 18px;
  }
}
</style>
